/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.facades.factories;

import com.sdd.entities.Category;
import com.sdd.plains.CategoryPlain;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

/**
 *
 * @author tpgalan
 */
@Stateless
@LocalBean
public class CategoryFactoryFacadeBean {

    public CategoryPlain createCategoryPlain(Category entity) {
        CategoryPlain ret = new CategoryPlain();
        ret.setName(entity.getName());
        ret.setCategoryId(entity.getId());
        List<CategoryPlain> childrenList = new ArrayList<CategoryPlain>();
        List<Category> sons = entity.getSon();
        if (sons != null) {
            for (Category category : sons) {
                childrenList.add(createCategoryPlainWithoutChildren(category));
            }
        }
        ret.setSubcategories(childrenList);
        return ret;
    }

    private CategoryPlain createCategoryPlainWithoutChildren(Category entity) {
        CategoryPlain ret = new CategoryPlain();
        ret.setName(entity.getName());
        ret.setCategoryId(entity.getId());
        return ret;
    }
    
    public List<CategoryPlain> createCategoriesPlains(List<Category> entities){
        List<CategoryPlain> ret = new ArrayList<CategoryPlain>();
        for (Category category : entities) {
            ret.add(createCategoryPlain(category));
        }
        return ret;
    }
}
